﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DelegateStudy
{
    //主要学习委托和事件分别作为类对象时的使用差异

    class Delegate {
        public Action action;
        public Func<String, String> func;

        public void AddDelegate(Action action, Func<String, String> func) {
            this.action += action;
            this.func += func;
        }
    }

    class Incident {
        //注意事件即使访问修饰限定符为public,但是也不能在类外直接进行赋值操作
        //但是可以进行+-运算(多播事件)，且也不能在类外直接进行调用
        public event Action action;
        public event Func<String, String> func;
        public void AddIncident(Action action, Func<String, String> func)
        {
            this.action += action;
            this.func += func;

        }
        public  void DoIncident()
        {
            action.Invoke();
            func.Invoke("incident");
        }
    }

    class IncidentDelegateClass
    {
        static void Main2() {
            Delegate delegate1 = new Delegate();
            Incident incident1 = new Incident();
            delegate1.AddDelegate(f1, f2);
            delegate1.action();
            delegate1.func("delegate");
            Console.WriteLine("===========");
            incident1.AddIncident(f1, f2);
            //incident1.action();事件不允许在类外进行调用
            incident1.action += f1;
            incident1.func += f2;
            incident1.DoIncident();
        }

        static void f1() {
            Console.WriteLine("fff  111");
        }

        static string f2(String input) {
            return input; 
        }
    }
}
